<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="../babel.min.js"></script>
    <script src="../react.development.js"></script>
    <script src="../react-dom.development.js"></script>
    <title>ConditionRendering条件渲染</title>
</head>

<body>
    <div id="root"></div>
    <script type="text/babel">
        function UserGreeting(props) {
            return <h1>Welcome Back!</h1>
        }
        function GuesteGreeting(props) {
            return <h1>Hello,Guest~</h1>
        }

        function Greeting(props) {
            if (props.isLoggedIn) {
                return <UserGreeting />
            }
            return <GuesteGreeting />
        }

        // ReactDOM.render(<Greeting isLoggedIn={true}/>, document.getElementById("root"))

        function LoginButton(props) {
            return <button onClick={props.onClick}>Login</button>
        }

        function LogoutButton(props) {
            return <button onClick={props.onClick}>Logout</button>
        }

        class LoginControl extends React.Component {
            constructor(props) {
                super(props)
                this.state = { isLoggedIn: true }
                // 由于是给onClick 设置回调，会改变 this的指向.或者使用箭头函数绑定 组件 this
                this.handleLogin = this.handleLogin.bind(this)
                // this.handleLogout = this.handleLogout.bind(this)
            }

            handleLogin() {
                console.log("hi")
                this.setState({ isLoggedIn: true })
            }

            handleLogout() {
                console.log("his")
                this.setState({ isLoggedIn: false })
            }
            render() {
                const logged= this.state.isLoggedIn
                if (logged)
                
                    return <div>
                        <Greeting isLoggedIn={logged} />
                        <LogoutButton onClick={()=>this.handleLogout()} />
                    </div>

                return <div>
                    <Greeting isLoggedIn={logged} />
                    <LogoutButton onClick={this.handleLogin} />
                </div>
            }
        }

        ReactDOM.render(<LoginControl />, document.getElementById("root"))
    </script>

</body>

</html>